Page({
    data: {
      postType: '寻找爱宠',
      filter: '全部',
      petList: []
    },
  
    onLoad() {
      this.fetchPetPosts();
    },
    onShow(){
        this.fetchPetPosts();
    },
    fetchPetPosts() {
      wx.showLoading({ title: '加载中...' });
      wx.request({
        url: 'http://localhost:3000/posts/petPosts',
        method: 'GET',
        success: (res) => {
          if (res.data.code === 200) {
            console.log('接口返回数据:', res.data.data);
            this.setData({
              petList: res.data.data.map(item => ({
                type: item.post_type,
                name: item.name,
                desc: item.description,
                city: item.city,
                views: item.views,
                image: item.image_urls[0] || '/images/default.png'
              }))
            });
            wx.hideLoading()

          } else {
            wx.showToast({ title: res.data.message || '查询失败', icon: 'none' });
          }
        },
        fail: () => {
            wx.hideLoading()
          wx.showToast({ title: '请求失败', icon: 'none' });
        }
      });
      
    },
  
    goBack() {
      wx.navigateBack();
    },
  
    setPostType(e) {
      this.setData({ postType: e.currentTarget.dataset.type }, () => {
        this.fetchPetPosts();
      });
    },
  
    setFilter(e) {
      this.setData({ filter: e.currentTarget.dataset.type }, () => {
        this.fetchPetPosts();
      });
    },
  
    goToPublish() {
        const userInfo = wx.getStorageSync('userInfo');
        if (!userInfo || !userInfo.openid) {
          wx.showToast({ title: '请先登录', icon: 'none' });
          return;
        }
      wx.navigateTo({ url: '/pages/pet-seek-publish/pet-seek-publish' });
    }
  });
  